﻿using DemoForBillOcr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaxAssistantBase;

namespace BillHelper
{
    public partial class MingXiInfo : Form
    {
        private IList<invoiceOcrDetailed> dclist;
        private IList<invoiceOcrDetailed> dcEndlist=new List<invoiceOcrDetailed>();
        private IList<InvoiiceOcrModel> currentPiaoJu;
        private AdminJM ajm;
         WsdLogger wlog = new WsdLogger("GuanLiLog");
        public MingXiInfo()
        {
            InitializeComponent();
        }

        public MingXiInfo(IList<invoiceOcrDetailed> dclist,IList<InvoiiceOcrModel> currentPiaoJu,AdminJM ajm)
        {
            this.dclist = dclist;
            this.ajm = ajm;
            this.currentPiaoJu = currentPiaoJu;
            InitializeComponent();
        }
        private void MingXiInfo_Load(object sender, EventArgs e)
        {  
            BindingList<invoiceOcrDetailed> bdlist = new BindingList<invoiceOcrDetailed>(dclist);
            mingxiTanChuTable.DataSource = bdlist;
        }
        private void getMingDataList() {
            ajm.getPiaoJuMingXi(currentPiaoJu,true, dclist);
        }
        private void getMingXiData(string strcolumn, string value,string id)
        {
            for (int i=0;i< dclist.Count; i++) {
                if (id == Convert.ToString(dclist[i].id))
                {
                    if (strcolumn == "productName")
                    {
                        dclist[i].productName = value;
                    }
                    if (strcolumn == "moneyAmount")
                    {
                        dclist[i].moneyAmount = value;
                    }
                    if (strcolumn == "detaileTaxRate")
                    {
                        dclist[i].detaileTaxRate = value;
                    }
                    if (strcolumn == "taxAmount")
                    {
                        dclist[i].taxAmount = value;
                    }
                    if (strcolumn == "unitPrice")
                    {
                        dclist[i].unitPrice = value;
                    }
                    if (strcolumn == "mxNumber")
                    {
                        dclist[i].mxNumber = value;
                    }
                    dcEndlist.Add(dclist[i]);
                    break;
                }
            }
            
           

        }

        private void mingxiTanChuTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var imingxi = sender as DataGridView;
            string temp = "";
            if (e.RowIndex != -1)
            {
                string strcolumn = imingxi.Columns[e.ColumnIndex].DataPropertyName;//获取列标题
                string strrow = imingxi.Rows[e.RowIndex].Cells[6].Value.ToString();//获取焦点触发行的第一个值
                string value = imingxi.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
                getMingXiData(strcolumn, value, strrow);
                string apiurl = ApiUrlList.UPDATEDETAILED;//更新明细
                try
                {
                    var result = RestOrcTool.Post(out temp, dcEndlist[0], apiurl);
                }
                catch (Exception ex)
                {
                    wlog.Write("更新明细表出错：" + ex.Message);
                }
                for (int j = 0; j < currentPiaoJu.Count; j++)
                {
                    for (int i = 0; i < currentPiaoJu[j].invoiceOcrDetailedDtoList.Count; i++)
                    {
                        if (currentPiaoJu[j].invoiceOcrDetailedDtoList[i].id == dclist[0].id)
                        {
                            currentPiaoJu[j].invoiceOcrDetailedDtoList[i] = dclist[0];
                        }
                    }
                }
                getMingDataList();
            }
        }
    }
}
